package com.example.callfor.api;

import com.example.callfor.dto.CallForBidsDto;
import com.example.callfor.model.CallForBids;
import com.example.callfor.model.CallForBidsMapper;
import org.springframework.beans.BeanUtils;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.List;

@RestController
@RequestMapping("/unit")
public class CallForBidsApi {

    @Resource
    private CallForBidsMapper callForBidsMapper;

    @PostMapping("/add")
    public Object add(String name, String type, String packing, Integer number, String deadline,Integer bail){
        try{
            CallForBids callForBids = new CallForBids();
            callForBids.setName(name);
            callForBids.setType(type);
            callForBids.setPacking(packing);
            callForBids.setNumber(number);
            callForBids.setDeadline(deadline);
            callForBids.setBail(bail);
            callForBidsMapper.addInfo(callForBids);
            return 1;
        }catch (Exception e){
            e.printStackTrace();
            return -1;
        }
    }

    @GetMapping("/getAll")
    public List<CallForBidsDto> getAll(){
        List<CallForBids> all = callForBidsMapper.getAll();
        List<CallForBidsDto> res = new ArrayList<>();
        for (CallForBids c :all) {
            CallForBidsDto callForBidsDto = new CallForBidsDto();
            BeanUtils.copyProperties(c,callForBidsDto);
            callForBidsDto.setId(c.getId().toString());
            callForBidsDto.setNumber(c.getNumber().toString());
            callForBidsDto.setBail(c.getBail().toString());
            res.add(callForBidsDto);
        }
        return res;
    }

    @PostMapping("/update")
    public Object update(CallForBids callForBids){
        return callForBidsMapper.updateInfo(callForBids);
    }
}
